package com.usiful.controller;

import java.io.File;
import java.io.IOException;

import com.usiful.model.ActionArg;
import com.usiful.utils.PathUtil;
import com.usiful.utils.PropertiesUtil;

/**
 * 全局属性类
 * 
 * @author [周织能]
 *
 */
public class GlobalSettingController extends BaseController {

	public final static String ARG_PROJECT_PATH = "path";
	public final static String ARG_OPERATE_TYPE = "operateType";
	public final static String ARG_KEY = "key";
	public final static String ARG_VALUE = "value";

	protected String mProjectPath;
	// 属性文件的路径
	protected String confFileName = "local.properties";
	protected PropertiesUtil prop = null;

	public GlobalSettingController() {

	}
	
	@Override
	public boolean handleAction(ActionArg arg) {
		// TODO Auto-generated method stub
		super.handleAction(arg);
		mProjectPath = arg.getArgValueMap().get(ARG_PROJECT_PATH).get(0);
		String operateType = arg.getArgValueMap().get(ARG_OPERATE_TYPE).get(0);
		String key = arg.getArgValueMap().get(ARG_KEY).get(0);
		String confFilePath = PathUtil.getFullPath(mProjectPath,confFileName);
		File confFile = new File(confFilePath);
		if (!confFile.getParentFile().exists()) {
			confFile.getParentFile().mkdirs();
		}
		if(!confFile.exists()) {
			try {
				confFile.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		prop = PropertiesUtil.getInstance(confFilePath);
		if(operateType.equals("set")) {
			String value = arg.getArgValueMap().get(ARG_VALUE).get(0);
			prop.set(key, value);
		} else if(operateType.equals("get")){
			
		}
		return true;
	}

	
}
